I am working on CakePHP and I have a URL http://admin.example.com/Pages .
How can I create http://admin.example.com/Pages.html ? Is there any solution or component to solve this issue?
According to CakeBook , You can define extensions you want to parse in routes
E.g. Write the code below in app/Config/routes.php
Router::parseExtensions('html');
This will allow you to send .html extenstion in routes(url)
Not create a link
E.g:
$this->Html->link('Link title', array(
'controller' => 'pages',
'action' => 'index',
'ext' => 'html'
));
When you click that link you will get url in browser something like this
http://admin.example.com/pages/index.html
Do you really need this? CakePHP is automatically render view files from View folder
You can create (if its PagesController and index methond) index.ctp in app/View/Pages folder and it will be automatically render.
You can use file_get_contents function to read files. Something like this:
// in controller
function index() {
$content = file_get_contents('path/to/file.html');
echo $content;
die();
}
Related
In CakePHP 2.x there is a HTML Helper which allows for creating hyperlinks within Views.
If I use this...
<?php
echo $this->Html->link('Navigation',
array('controller' => 'navigations', 'action' => 'foo')
);
?>
... it will generate a URL to /navigations/foo (NavigationsController::foo).
However if I use index as the action
<?php
echo $this->Html->link('Navigation',
array('controller' => 'navigations', 'action' => 'index')
);
?>
The URL becomes /navigations.
I understand about "index" being a default when it comes to webpages. However, I actually need the URL to be /navigations/index, or at the very least have a trailing slash (/navigations/).
Is this possible?
Fix the AJAX URLs instead
With regards to the explanation in your comment, I would argue that the "correct" way of fixing this problem is to use proper root relative URLs for the AJAX calls, as changing the URL structure only cloakes the underlying problem, that is targeting non-unique resources.
Personally I alwas use Router::url() to generate the URLs, either in the specific script files when serving them via PHP, or by writing out configuration in for example the layout, being it globally so that the scripts can access it when needed:
<script>
var config = {
navigationDataUrl: <?php echo json_encode(
Router::url(array('controller' => 'Navigations', 'action' => 'get_data'))
) ?>
};
</script>
or by configuring possibly existing JS objects.
<script>
navigation.dataUrl = <?php echo json_encode(
Router::url(array('controller' => 'Navigations', 'action' => 'get_data'))
) ?>;
</script>
Connect routes with an explicit index part
That being said, for the sake of completion, it is possible to force the index part to not be dropped, by connecting routes that explicitly define that part, as opposed to using the :action route element, like:
Router::connect('/:controller/index', array('action' => 'index'));
which would match/catch URL arrays like yours before they are being handled by CakePHPs default controller index catchall route, which looks like:
Router::connect('/:controller', array('action' => 'index'), array('defaultRoute' => true));
https://github.com/cakephp/cakephp/blob/2.10.6/lib/Cake/Config/routes.php#L72
In my CakePHP 3 application, after a controller redirect I get this error:
Error: [LogicException] Controller action can only return an instance of Response
Request URL: /mycontroller/
Stack Trace:
#0 /var/www/vhosts/example.com/httpdocs/vendor/cakephp/cakephp/src/Routing/Dispatcher.php(87): Cake\Routing\Dispatcher->_invoke(Object(App\Controller\SigninsController))
#1 /var/www/vhosts/example.com/httpdocs/webroot/index.php(37): Cake\Routing\Dispatcher->dispatch(Object(Cake\Network\Request), Object(Cake\Network\Response))
#2 {main}
Regarding to CakePHP 2 controller documentation redirect was like this:
$this->redirect('/orders/thanks');
$this->redirect('http://www.example.com');
return $this->redirect(
array('controller' => 'orders', 'action' => 'confirm')
);
But in CakePHP 3 documentation it seems like this:
return $this->redirect('/orders/thanks');
return $this->redirect('http://www.example.com');
return $this->redirect(
['controller' => 'Orders', 'action' => 'thanks']
);
When I add return word before $this->redirect, error is solved. So does this makes problem ? Because I couldn't see a return redirect part in cakephp 3 migration guide. Migration guide only mentions that third parameter is dropped.
Return in CakePHP 3 application is used when you want to redirect to other pages or we can say actions/methods of other controllers or even if you want to redirect to 3rd Party Links like you have specified :-
return $this->redirect('/orders/thanks');
return $this->redirect('http://www.example.com');
return $this->redirect(['controller' => 'Orders', 'action' => 'thanks']);
And if you want to redirect to other method in the same controller then you can leave the return and use setAction as below but here URL will remain the same.
$this->setAction('thanks');
Return is better approach and URL will redirect if you use below code.
return $this->redirect(['action' => 'thanks']);
Take a look at this
Response provides an interface to wrap the common response-related tasks such as:
Sending headers for redirects.
and this
You should return the response instance from your action to prevent view rendering and let the dispatcher handle actual redirection.
if you are using route name then use this-
$this->redirect(['_name' => 'view-details']);
you are using route then use this-
$this->redirect('/page/view-details');
and last option is this
$this->redirect(['controller' => 'Pages', 'action' => 'view']);
----------------------------------------------------------------------
if you are use route name in view file then this use-
use Cake\Routing\Router;
echo Router::url(['_name' => 'view-details']);
echo Router::url(['controller' => 'Articles', 'action' => 'view', 'id' => 15]);
In cakephp, 3.6 to go to specific view/page Use $this->render('filename');
like below
public function search()
{
$this->render();
$this->render('profile');
}
You need to turn off Autorendering
since cakephp methods by default want to end with a redirect to a CTP file
use $this->autoRender = false
inside you function
I have a function in my application which the users can upload files to the webserver. Then these uploaded files will appear in another page wherein another type of users can click on the link. Once the link is clicked, a new tab will open and the file will be shown.
But I can't seem to do it. Using the 'target' => '_blank' is not working, or I may have put it on the wrong part of the code.
In my case, when you click on the link, the file will load on the same tab.
Here's my code:
<?php
echo $this->Html->link($staff_uploads['StaffUpload']['title'], array(
'controller' => 'websites',
'action' => 'view',
'target' => '_blank',
$staff_uploads['StaffUpload']['iduploads']
)
);
?>
Thank you in advance!
The correct code is:
<?php
echo $this->Html->link($staff_uploads['StaffUpload']['title'], array(
'controller' => 'websites',
'action' => 'view',
$staff_uploads['StaffUpload']['iduploads']
), array('target' => '_blank')
);
?>
And do read the documentation as burzum has suggested.
Read the documentation.
HTML attribute options go into the 3rd argument of the link() method, not the second which is the URL as string or array.
Problems like this can be simply resolved by using the documentation.
I am making blog and url route is like this-
Router::connect('/blog/c/:catid/*',
array('controller' => 'blogarticles', 'action' => 'index'));
it works well with url as- /blog/c/3/other-articles
but when i use paginator in view as
echo $this->Paginator->numbers();
it generates url as- /blogarticles/index/other-articles/page:2
What changes should in make in paginator to generate proper url.
Please suggest possible solution , Thanks in advance
This should solve your problem:
$this->Paginator->options(
array(
'controller' => 'blog',
'action' => 'c',
$catid,
$title
)
);
The trick is to pass blog as the controller and c as the action, and all other variables (NOT LIMITED TO $catid and $title) as additional parameters, sequentially!
NOTE: I assumed here that you have "set" $catid and $title from your Controller, to the current "category id" and "title" respecting. I also assumed that your URLs are always in the format: /blog/c/:catid/:title
You may also want to view my answer to a similar question: https://stackoverflow.com/a/25097693/2862423
What you want is to set the options for the PaginatorHelper for that view:
<?php $this->Paginator->options(array('url' => '/blog/c/3/other-articles')); ?>
CakePHP Book Section on the PaginatorHelper Options Function: http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#modifying-the-options-paginatorhelper-uses
have custom pagination in my cakephp view. before that i made some custom routing changes.
problem is that links leads to pages like
http://localhost/myapp/foos/view/news/page:2
instead of
http://localhost/myapp/news/page:2
so, part with foos/view/ not have to be part of the link.
tried to change url with several custom options, like
$this->Paginator->options(array('url' => $this->passedArgs));
but no luck, because i always have foos/view/ in url.
can you help me how can i get rid of that foos/view?
thank you very much in advance!
UPDATE: i manage to do "something", but not enough, by adding following lines:
$options = array('url'=> array('controller' => 'news' ) );
$paginator->options($options);
now, my link looks like:
http://localhost/myapp/news/index/page:2
how can i get rid of that "index" in url?
The following line is more about passing various pieces of URL information to the view:
$this->Paginator->options(array('url' => $this->passedArgs));
I think what you want to look into is the helper declaration in your Controller:
var $helpers = (
'SomeHelper',
'AnotherHelper',
'Paginator' => array(
'url' => array('controller'=>'news')
)
);
If you want finer control of a custom route like the one you have then try
'url' => '/news'
I haven't used PaginatorHelper in a while - so I could be egregiously on the wrong track - but I believe that's a good start.
Also, take a look at the Paginator Helper page for where it mentions $options and then take a look at Router::url() as the former page recommends.
I had a case where I am working on a project using CakePHP 2.1 (This thread is tagged as 1.3) with a dynamic admin route to display pages like this:
Router::connect('/admin/main/*', array('controller' => 'adminPages', 'action' => 'display'));
With a query string parameter, that produces a dynamic url like this: http://mydomain.com/adminPages/main/...?page=1
The link route, was incorrect for our needs and found I could alter the url directly by using this:
$this->Paginator->options(array(
'url' => array(
'controller' => 'admin/main/my-display',
)
));
For me it made a link: http://mydomain.com/admin/main/my-display?page=1 - which was the correct url we were looking for. If I used a string, as described above, it appends itself to the url, like: http://mydomain.com/adminPages/main/.../admin/main/my-display?page=1
In view :
<?php
$this->Paginator->options(array('url' => array('controller' => '','action' =>'your-custom-url')));
?>
In routes.php :
<?php
Router::connect('/your-custom-url/*', array('controller' => 'Controller', 'action' => 'function'));
?>