I have a page in the database. I want to have a URL like mysite.com/page_alias for the page. How do I properly write route in laravel 4.
In kohana i did this:
Route::set('static', '<page>', array('page' => "page|page2|page3|etc"))
->defaults(array(
'action' => 'index',
'controller' => 'Static',
'directory' => 'Index',
));
Thanks.
Sorry for my english.
I never use Kohana but looking at its doc, if I'm right, this is the closest way to do what you used to do with Kohana.
Route::get('{page}', 'StaticController#index')->where('page', 'page|page2|page3|etc');
PS: take a look at this http://laravel.com/docs/4.2/routing (you probably have already checked the doc but in case..)
Related
i want to rewrite my URLs from
/view?id=100
to
/view/100-article-title
But the site already has several thousand search pages. As far as I know, such a change can be bad for SEO. Is there a way to keep the old URLs working when switching to the new routing.
*I am creating links as follows:
Url::to(['view', 'id' => $item->id])
Is it possible to create links further through ID?
you can create getLink() function on your model and use it on. Then, when function runs you can check id if id <= 100 then return Url::to(['view', 'id' => $item->id]) else return Url::to(['view', 'id' => $item->id, 'slug' => $this->slug])
And add route with slug on your main.php
Look at my example.
First config urlManager in config.php in 'app/config' folder. In my case look like:
'components' => [
...
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
''=>'home/index',
'<slugm>-<id:\d+>-' => 'home/view',
],
],
.
...
],
and create link as fallow:
Url::to(['/home/view', 'id' => $model->home_id, 'slugm' =>$model->title_sr_latn])
and finaly url look like:
https://primer.izrada-sajta.rs/usluge-izrade-sajtova-1-
I was looking to change controller name in URL. Which, we can do by renaming the controller name in module. But, Through URL manager if we can do it. It will be better.
Module: user,
Controller: api,
Action: index
Right now,
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:(api)>/<action:\w+>/<id:[a-z0-9]+>' => 'user/<controller>/<action>',
'<controller:(api)>/<action>' => 'user/<controller>/<action>',
]
];
And, I can access it through.
http://dev.example.com/api/index
But, I was looking to change it to
http://dev.example.com/world/index
How can I do it? Any help/hint/suggestion is appreciable.
You can create custom url rules by adding items to the rules array.
So, in your case insert this into the rules array
'world/index' => 'api/index'
You can read more about URL rules here.
also you use ControllerMap
it useful when you are using third-party controllers and you do not have control over their class names.
below code in component in main.php in advance or web.php in basic
for example:
'controllerMap' => [
'api' => 'app\controllers\WorldController',
]
The following code:
{{url('/'.$subjecttype->name)}}
is the name 'garden' wrapped up in a url. This gives me localhost/garden with obviously garden as the dynamic name. With my routes setup like so:
Route::get('/{subject}/', array( 'as' => 'subject', 'uses' =>
'SubjectController#getsubject'));
The question is how would I setup two dynamic names within one route? For example
localhost/garden/12
so i would want my route to look something like this
Route::get('/{subject}/{id}/', array( 'as' => 'subjectid', 'uses' => 'SubjectController#getsubjectid'));
but more importantly what would it look like in my view? so that I have the of my garden header wrapped up in a url that looks like this:
'gardening tips for beginners' which is {{$subjecttype->title}}
below is my very poor attempt at what i want but i hope you get the picture.
{{url('/$subjecttype->name/$subjecttype->id/'.$subjecttype->title)}}
Thanks
For your route:
Route::get(
'/{subject}/{id}/',
array(
'as' => 'subjectid',
'uses' => 'SubjectController#getsubjectid'
)
);
you can generate the URL with the following code:
$url = URL::route(
'subjectid',
array(
'subject' => $subjecttype->name,
'id' => $subjecttype->id
)
);
or, if you prefer to use the helper functions:
$url = route(
'subjectid',
array(
'subject' => $subjecttype->name,
'id' => $subjecttype->id
)
);
That's going to give you a URL like http://example.com/subjectname/42. If you want to add another parameter like the title at the end of the URL, you'll need to add another parameter to your route definition. If you don't you're going to get 404 errors because no route will match the URL.
For the second part of my question using the 'gardening tips for beginners' example:
gardening tips for beginners
I looked around to find answers and though I found topics about the routes, none of the answers I found worked in this case. So here I go, trying to explain my problem =]
Background information
I'm making a website in Cakephp for an estate agent. On this website you have the possibility to sort houses based on the street, publish date and price.
What I want
I want to change these current urls:
websitename.nl/houses/index/page:4/sort:street/direction:desc
websitename.nl/houses/index/sort:street/direction:desc/page:4
to something like:
websitename.nl/houses/street/descending/4
websitename.nl/houses/price/ascending/4
Street/price and descending/ascending will be translated to dutch.
What I already tried
I tried to add this in routes.php:
Router::connect('/huizenaanbod/datum/aflopend/:page', array('controller' => 'houses', 'action' => 'index', 'sort'=>'published', 'direction' => 'desc'), array('page' => '[0-9]+'));
Router::connect('/huizenaanbod/datum/oplopend/:page', array('controller' => 'houses', 'action' => 'index', 'sort'=>'published', 'direction' => 'asc'), array('page' => '[0-9]+'));
But it ignored the desc and only the asc worked.. So I tried to add this at the index.ctp:
if((!empty($this->params['sort']) && $this->params['sort'] == "published") && $this->params['direction'] == 'desc'){
echo $this->Paginator->sort('published', 'datum', array('direction' => 'asc', 'escape' => false));
} else {
echo $this->Paginator->sort('published', 'datum', array('direction' => 'desc', 'escape' => false));
}
And eventhough the url does change now, the results of the sort are still ordered ASC.
So my question is:
How can I make an url in routes, combined with :page, for a specific sort and direction?
If you need more information, let me know.
And thank you in advance =]
you better look at other web agencies web site to see how they do the filtering. look at the link below see how the parameters is passed.. e.g.
http://www.rightmove.co.uk/property-for-sale/find.html?searchType=SALE&locationIdentifier=REGION%5E92826&insId=1&radius=0.0&displayPropertyType=&minBedrooms=&maxBedrooms=3&minPrice=&maxPrice=&retirement=&partBuyPartRent=&maxDaysSinceAdded=&_includeSSTC=on&sortByPriceDescending=&primaryDisplayPropertyType=&secondaryDisplayPropertyType=&oldDisplayPropertyType=&oldPrimaryDisplayPropertyType=&newHome=&auction=false
Now lets get back to you question.. for passing all the parameters you need to use /** e.g.
Router::connect(
'/action_name/**', //once you requesting rather than star you need to put your parameters
array('controller' => 'pages', 'action' => 'show')
);
//www.exsmple.com/action_name/parameters here...
Reference
you can also customize your pagination class from here
just posted this on IRC channel ZFTalk too,
Hope I can get some help on ZF2, ZF2 Album Tutorial, OSX using MAMP. Skeleton framework, homepage is working.
Issue : After completing section : 8.5 Listing albums, you fill up the module/Album/view/album/album/index.phtml with some code, then they ask you to preview the page on http://zf2-tutorial.localhost/album.
I get a 404, The requested URL could not be matched by routing.
I headed to Google for advice. Found a GIT repository with a 'fully working model' of the Tutorial, so i got this to compare my code with. If i set up this as another host I get the same 404 routing message.
After carefully studying the manual, it explicitly states in the start that you will not be able to view anything other than the start/home page if your httpd.conf / AllowOverride is not set to FileInfo.
Decided to scan the whole machine for files called httpd.conf, just for in case the path to the one I changed is not used by MAMP when powering up the server.
So found 3, changed all of them (Although 3 we're found, I believe the correct route is /private/etc) My problem still exists in the code i wrote from the tutorial, as well as the GIT code of the 'working model'.
Has anyone encountered issues with this? found this on stackoverflow Zend Framework 2 .htaccess mamp pro which has similarities to my problem but has not resolved it. Can anyone in here help me?
Other routes taken involve : Checking for spelling mistakes in the code, checking the application.config.php has a route set up. Please advise? :)
Module.php
<?php
namespace Album;
use Album\Model\Album;
use Album\Model\AlbumTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
class Module
{
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getServiceConfig()
{
return array(
'factories' => array(
'Album\Model\AlbumTable' => function($sm) {
$tableGateway = $sm->get('AlbumTableGateway');
$table = new AlbumTable($tableGateway);
return $table;
},
'AlbumTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Album());
return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
},
),
);
}
}
module.config.php
<?php
return array(
'controllers' => array(
'invokables' => array(
'Album\Controller\Album' => 'Album\Controller\AlbumController',
),
),
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\Album',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
),
),
);
Resolved this error with help from this thread.
I think, barring any other misconfiguration, the source of this error - for me at least - was where to register the Album module.
The SKELETON Application comes with an Application MODULE. Those are two different things and they have have their own config folders:
config // SKELETON Application config
module/Application/config // Application MODULE config
The module needs to be registered in the Skeleton Application config file provided with the skeleton, namely config/application.config.php and NOT by creating an application.config.php file in the Application Module config, e.g. module/Application/config/application.config.php.
You can solve is by configuring the apache settings in (httpd.conf) change the
"AllowOverride None" to "AllowOverride All".
Such setting permit you to override the config value by .htaccess
Please try this
If you see a standard Apache 404 error, then you need to fix .htaccess usage before continuing. If you’re are using IIS with the URL Rewrite Module, import the following:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php [NC,L]
You now have a working skeleton application and we can start adding the specifics for our application. Please let me know if it worked fine from you.